package myEddy.core;

import static org.junit.Assert.*;

import java.util.LinkedList;

import myEddy.operator.Join;
import myEddy.operator.Select;
import myEddy.tuple.Tuple;

import org.junit.Before;
import org.junit.Test;

public class EddyTest {

	Eddy E;
	
	@Before
	public void setUp() throws Exception {
		E = new Eddy();
	}

	@SuppressWarnings("static-access")
	@Test
	public void testJoin() throws Exception {
		Join J = new Join("A", "B", "name", "name2");
		E.addOperator(J);
		
		//Starting thread
		new Thread(E).start();
		
		//Sending two tuples
		Tuple A = new Tuple("A");
		A.addElement("idA", "1");
		A.addElement("name", "sparte");
		
		Tuple B = new Tuple("B");
		B.addElement("idB", "2");
		B.addElement("name2", "sparte");
		
		E.addTuple(A);
		E.addTuple(B);
		
		//Waiting for the job to be done...
		try {
			Thread.currentThread().sleep(500);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
		//Checking the output
		LinkedList<Tuple> res = E.getOutput();
		
		if(0 < res.size() && res.size() < 2 )
		{
			assertEquals(res.get(0).getElement("idA"), "1");
			assertEquals(res.get(0).getElement("idB"), "2");
		}
		else
		{
			fail("Wrong number of result !");
		}
	}
	
	@SuppressWarnings("static-access")
	@Test
	public void testSelect() throws Exception{
		Select S = new Select("A", "name", "sparte");
		E.addOperator(S);
		
		//Starting thread
		new Thread(E).start();
		
		//Sending two tuples
		Tuple A = new Tuple("A");
		A.addElement("idA", "1");
		A.addElement("name", "sparte");
		
		Tuple B = new Tuple("A");
		B.addElement("idA", "2");
		B.addElement("name", "spartaprout");
		
		E.addTuple(A);
		E.addTuple(B);
		
		//Waiting the process to do the job
		try {
			Thread.currentThread().sleep(200);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
		//Reading the output
		LinkedList<Tuple> res = E.getOutput();
		
		if(0 < res.size() && res.size() < 2 )
		{
			assertEquals(res.get(0).getElement("name"), "sparte");
		}
		else
		{
			System.out.print(res.size());
			fail("Wrong number of result !");
		}
	}
	
	@SuppressWarnings("static-access")
	@Test
	public void testCombine() throws Exception{
		Select S = new Select("A", "idA", "1");
		E.addOperator(S);		
		
		Join J = new Join("A", "B", "name", "name2");
		E.addOperator(J);
		
		//Starting thread
		new Thread(E).start();
		
		//Sending two tuples
		Tuple A = new Tuple("A");
		A.addElement("idA", "1");
		A.addElement("name", "sparte");
		
		Tuple A2 = new Tuple("A");
		A2.addElement("idA", "2");
		A2.addElement("name", "sparte");
		
		Tuple B = new Tuple("B");
		B.addElement("idB", "2");
		B.addElement("name2", "sparte");
		
		E.addTuple(A);
		E.addTuple(A2);
		E.addTuple(B);
		
		//Waiting the process to do the job
		try {
			Thread.currentThread().sleep(200);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
		//Reading the output
		LinkedList<Tuple> res = E.getOutput();
		
		if(0 < res.size() && res.size() < 2 )
		{
			assertEquals(res.get(0).getElement("idA"), "1");
		}
		else
		{
			System.out.print(res.size());
			fail("Wrong number of result !");
		}
	}
	
	@SuppressWarnings("static-access")
	@Test
	public void testDoubleJoin() throws Exception {
		Join J = new Join("A", "B", "nameA", "nameB");
		E.addOperator(J);
		
		Join J2 = new Join("B", "C", "ageB", "ageC");
		E.addOperator(J2);
		
		
		
		//Starting thread
		new Thread(E).start();
		
		//Sending two tuples
		Tuple A = new Tuple("A");
		A.addElement("idA", "1");
		A.addElement("nameA", "sparte");
		
		Tuple B = new Tuple("B");
		B.addElement("idB", "2");
		B.addElement("nameB", "sparte");
		B.addElement("ageB", "20");
		
		Tuple B2 = new Tuple("B");
		B2.addElement("idB", "1");
		B2.addElement("nameB", "sparte");
		B2.addElement("ageB", "15");
		
		Tuple C = new Tuple("C");
		C.addElement("idC", "10");
		C.addElement("ageC", "20");
		
		E.addTuple(A);
		E.addTuple(B);
		E.addTuple(B2);
		E.addTuple(C);
		
		//Waiting for the job to be done...
		try {
			Thread.currentThread().sleep(500);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
		//Checking the output
		LinkedList<Tuple> res = E.getOutput();
		
		if(0 < res.size() && res.size() < 2 )
		{
			assertEquals(res.get(0).getElement("idA"), "1");
			assertEquals(res.get(0).getElement("idB"), "2");
			assertEquals(res.get(0).getElement("idC"), "10");
		}
		else
		{
			fail("Wrong number of result !");
		}
	}

}
